package net.jxiny.oschina.synthetical.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.okhttplib.HttpInfo;
import com.okhttplib.OkHttpUtil;
import com.okhttplib.callback.Callback;

import net.jxiny.oschina.R;
import net.jxiny.oschina.net.URLList;
import net.jxiny.oschina.synthetical.Adapter.BlogAdapter;
import net.jxiny.oschina.synthetical.Adapter.ImgBlogAdapter;
import net.jxiny.oschina.synthetical.Result.BlogResult;
import net.jxiny.oschina.synthetical.enyity.Blog;
import net.jxiny.oschina.synthetical.enyity.Imger;
import net.jxiny.oschina.util.ACache;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class BlogFragment extends Fragment {
    @BindView(R.id.recyclerView_blog_detial)
    RecyclerView recyclerViewBlogDetial;
    private Unbinder binder;
    @BindView(R.id.recyclerView_blog)
    RecyclerView BlogView;
    private List<Blog> lists = new ArrayList<>();
    private List<Imger> imges = new ArrayList<>();
    private BlogAdapter mBlogAdapter;
    private RecyclerView BlogImg;

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_blog, container, false);
        binder = ButterKnife.bind(this, view);

        BlogImg = view.findViewById(R.id.recyclerView_blog);
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Blog blog = new Blog();
        OkHttpUtil.getDefault(this)
                .doGetAsync(HttpInfo.Builder().setUrl(URLList.GET_BLOG +
                        ACache.get(getActivity()).getAsString("token")).build(), new Callback() {
                    @Override
                    public void onSuccess(HttpInfo info) throws IOException {

                        BlogResult result = info.getRetDetail(BlogResult.class);
                        mBlogAdapter.replaceData(result.getBloglist());
                    }


                    @Override
                    public void onFailure(HttpInfo info) throws IOException {
                        Toast.makeText(getContext(),"发送失败"+info,Toast.LENGTH_LONG).show();
                    }
                });
        //*********************************************
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false);
        recyclerViewBlogDetial.setLayoutManager(manager);
        mBlogAdapter = new BlogAdapter(R.layout.item_blog, lists);
        recyclerViewBlogDetial.setAdapter(mBlogAdapter);

        //TODO 假数据， 后续改为网络请求**********************
        Imger imger = new Imger();
        for (int i = 0; i < 4; i++) {
            imges.add(imger);
        }
        //*********************************************
        RecyclerView.LayoutManager manager2 = new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false);
        BlogImg.setLayoutManager(manager2);
        BlogImg.setAdapter(new ImgBlogAdapter(R.layout.bbanner, imges));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binder.unbind();
    }


}